package com.fram.controller.admin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseAdminController;
import com.fram.entity.UserWithdrawCashList;
import com.fram.service.admin.WithdrawalService;

//提现中心
@RestController
@RequestMapping("/admin/{version}/auth/withdrawal")
public class WithdrawalController extends BaseAdminController {

	private static Logger LOG = LoggerFactory.getLogger(WithdrawalController.class);

	@Autowired
	private WithdrawalService withdrawalService;

	@ApiVersion(1.0)
	@RequestMapping("list")
	private ResponseMsg list(@RequestBody PageBean<UserWithdrawCashList> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if(getDistributorId() == null) {
				request.getParam().setUserId(null);
				msg.setData(withdrawalService.list(request));
			}else {
				request.getParam().setUserId(getAdminUserId());
				msg.setData(withdrawalService.list(request));
			}
		} catch (ProException e) {
			LOG.error("-申请提现出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-申请提现异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("detail")
	private ResponseMsg detail(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(withdrawalService.detail(id));
		} catch (ProException e) {
			LOG.error("-提现详情出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-提现详情异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("update")
	private ResponseMsg update(@RequestBody UserWithdrawCashList request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			withdrawalService.update(request);
		} catch (ProException e) {
			LOG.error("-提现修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-提现修改异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
